<template>
  <div>
    <SearchForm
        @search="onSearch"
        @reset="onReset"
        :search-list="searchList" :search-params="searchParams">
    </SearchForm>

    <div class="table-layout">
      <el-card shadow="never">
        <div class="table-handle">
          <el-button type="primary" size="mini" icon="el-icon-plus" @click="onAdd">新增</el-button>
        </div>
        <div class="table-box">
          <el-table
              size="large"
              stripe
              border
              v-loading="loading"
              max-height="500px"
              :data="tableData"
              row-key="id"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(255, 255, 255, 0.8)"
              @selection-change="onSelect"
          >
            <el-table-column label="产品编号" fixed="left" align="center" key="id" prop="id" />

            <el-table-column prop="is_recommend" fixed="left" label="是否推荐" width="150">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.is_recommend === 1" type="success">是</el-tag>
                <el-tag v-else type="danger">否</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="logo_url" label="产品logo" width="150">
              <template slot-scope="scope">
                <el-image
                    :preview-src-list="[scope.row.logo_url]"
                    style="width: 100px; height: 100px"
                    :src="scope.row.logo_url" fit="contain"></el-image>
              </template>
            </el-table-column>
            <el-table-column prop="name" label="产品名称" width="150"/>
            <el-table-column prop="category_id" label="产品分类ID" width="150"/>
            <el-table-column prop="max_amount" label="最高额度" width="150"/>
            <el-table-column prop="min_annual_rate" label="最低年化利率(%)" width="150"/>
            <el-table-column prop="max_annual_rate" label="最高年化利率(%)" width="150"/>
            <el-table-column prop="min_term" label="最小期限(月)" width="150"/>
            <el-table-column prop="max_term" label="最大期限(月)" width="150"/>
            <el-table-column prop="description" label="产品简介" width="150"/>
            <el-table-column prop="status" label="启用状态" width="150">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.status === 1" type="success">启用</el-tag>
                <el-tag v-else type="info">禁用</el-tag>
              </template>
            </el-table-column>
            <el-table-column label="操作" fixed="right" width="140">
              <template slot-scope="scope">
                <el-button type="text" @click="onEdit(scope.row)">编辑</el-button>
                <el-button type="text" @click="onDelete(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-card>
      <div class="page-box">
        <el-pagination
            background
            :current-page="pageNo"
            :page-sizes="pageSizes"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalSize"
            @size-change="changePageSize"
            @current-change="changePageNo"
        />
      </div>
    </div>
    <Add ref="addFormRef" @get-page="initData" />
  </div>
</template>
<script>

import crud from "@/mixins/crud";
import { http } from "@/api";
import Add from "./add";

export default {
  name: "ProductIndex",
  components: {
    Add
  },
  mixins: [crud],
  data() {
    return {
      request: http.product,
      searchList: [
        {
          label: '产品名称',
          prop: 'name',
          name: 'input',
          clearable: true
        },
        {
          label: '是否推荐',
          prop: 'is_recommend',
          name: 'select',
          clearable: true,
          options: [
            { label: '是', value: 1 },
            { label: '否', value: 0 }
          ]
        }
      ],
      searchParams: {
        name: '',
        is_recommend: ''
      },
      defaultParams: {
      },
      tableData: [],
    }
  },
  methods: {
    onAdd() {
      this.$refs.addFormRef.open();
    },
    onEdit(row) {
      this.$refs.addFormRef.open(row);
    },
    onDelete(row) {
      this.$confirm('确认删除该产品吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.request.delete(row.id).then(() => {
          this.$message.success('删除成功')
          this.initData()
        })
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    }
  }
}
</script>

<style scoped lang="scss">
.table-layout {
  margin-top: 20px;
}
.table-handle {
  margin-bottom: 20px;
}
.page-box {
  margin-top: 20px;
  text-align: right;
}
</style>
